package FX;

import BasicClass.*;
import BookReadIO.BookWriter;
import Connection.*;
import DAO.DaoException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;


public class ManagerUI extends Application {
    static Stage login = null;//初始登录窗口
    static Stage ManagerStage = null;//主页面Stage
    static MyConnection Myconnection =null;//连接数据库
    static User CurUser = null;//当前用户
    static Label AccountName = null;//账户名称
    static BorderPane rootNode;//页面构图
    static int _Image_MAX_NUMBER_ = 10;//背景图片数
    static int _MAX_FACTION_ = 9;//最大功能数
    static int _CUR_BUTTON_FACTION = 0;//当前按钮功能数
    static int _CUR_BOX_FACTION = 0;//当前功能布局数
    static double _SCENE_WIDTH_ = 1460;//初始化窗口宽度
    static double _SCENE_HEIGHT_ = 780;//初始化窗口高度
    static final int _MAX_SHOW_COLUMNS_ = 20;//最大表格显示行
    static final int _TABLEVIEW_HEIGHT_ = 600;//表格高度
    static int _PAGE_BOOK_ = 1;//当前搜索书籍显示页码
    static int _PAGE_RECORD_ = 1;//当前搜索借阅显示页码
    static int _PAGE_ACCOUNT_ = 1;//当前搜索账号显示页码
    static int _PAGE_BILL_ = 1;//当前搜索账单显示页码
    static final String _BackgroundColor_ = "-fx-background-color: RGB(54, 174, 179)";//背景颜色
    static final String _BackgroundColorHighLight_ =  "-fx-background-color: RGB(72, 237, 242)";//背景颜色高亮
    static final Button[] Function = new Button[_MAX_FACTION_];//功能按钮
     static VBox[] FunctionBox = new VBox[_MAX_FACTION_];//功能布局

    @Override
    public void start(Stage primaryStage){
        //全局布局
        rootNode = new BorderPane();
        AnchorPane MenuLabel = new AnchorPane();
        MenuLabel.setStyle(_BackgroundColor_);
        MenuLabel.setPrefSize(300, 50);
        HBox AccountBox = new HBox();
        ImageView AccountImage = new ImageView("./fodder/用户.png");
        AccountImage.setFitHeight(35);
        AccountImage.setPreserveRatio(true);
        AccountName = new Label(String.format("当前用户: %s", getUser().getName()));
        AccountName.setStyle("-fx-font-size: 28");
        Button Logout = new Button("退出");
        Button Freshen = new Button("刷新");
        AccountBox.setSpacing(5);
        AccountBox.getChildren().addAll(AccountImage, AccountName, Logout, Freshen);
        //显示当前时间
        Label Time = new Label();
        Time.setStyle("-fx-font-size: 28");
        BasicClass.Time time = new Time();
        Thread getTime = new Thread(time, "Clock");
        getTime.start();
        Time.textProperty().bind(time.messageProperty());

        //添加顶部布局
        MenuLabel.getChildren().addAll(AccountBox, Time);
        AnchorPane.setLeftAnchor(AccountBox, 10.0);
        AnchorPane.setTopAnchor(AccountBox, 3.0);
        AnchorPane.setRightAnchor(Time, 30.0);
        AnchorPane.setTopAnchor(Time, 3.0);
        rootNode.setTop(MenuLabel);

        //添加主要功能按钮
        VBox ButtonBox = new VBox();
        ButtonBox.setStyle(_BackgroundColor_);
        Function[_CUR_BUTTON_FACTION++] = new Button("借阅书籍");
        Function[_CUR_BUTTON_FACTION++] = new Button("归还书籍");
        Function[_CUR_BUTTON_FACTION++] = new Button("购买书籍");
        Function[_CUR_BUTTON_FACTION++] = new Button("账号信息");
        Function[_CUR_BUTTON_FACTION++] = new Button("历史账单");
        Function[_CUR_BUTTON_FACTION++] = new Button("书籍管理");
        Function[_CUR_BUTTON_FACTION++] = new Button("账户管理");
        Function[_CUR_BUTTON_FACTION++] = new Button("账单管理");
        Function[_CUR_BUTTON_FACTION++] = new Button("关于系统");
        for(int i=0;i<_MAX_FACTION_;i++){
            if(Function[i].isVisible()) ButtonBox.getChildren().add(Function[i]);
            Function[i].setMinSize(_SCENE_WIDTH_/8, _SCENE_HEIGHT_/10);
        }
        ShowManageAction();
        rootNode.setLeft(ButtonBox);

        //借阅书籍
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++,1);
        rootNode.setCenter(FunctionBox[0]);

        //归还书籍
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 2);

        //购买书籍
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 3);

        //账户信息
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        AccountLayout(Myconnection, _CUR_BOX_FACTION++);

        //历史账单
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 5);

        //书籍管理
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 6);

        //账户管理
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 7);

        //账单管理
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 8);

        //关于系统
        FunctionBox[_CUR_BOX_FACTION] = new VBox();
        SystemInf(_CUR_BOX_FACTION++);

        //按键功能和动画效果
        for(int i=0;i<_MAX_FACTION_;i++) {
            int finalI = i;
            Function[i].setOnMouseClicked(e->{
                Function[finalI].setStyle(_BackgroundColorHighLight_);
                for (int j = 0; j < _MAX_FACTION_; j++) {
                    if(j!=finalI&Function[j].isVisible())Function[j].setStyle(_BackgroundColor_);
                }
                if (Function[finalI].isVisible()) rootNode.setCenter(FunctionBox[finalI]);
            });
        }

        //退出当前登录
        Logout.setOnMouseClicked(event -> {
            getLogin().show();
            Login.setFirstLogin(false);
            setManagerStage(primaryStage);
            primaryStage.hide();
        });
        //刷新点击
        Freshen.setOnMouseClicked(event -> UpdateWindows(rootNode));
        //时间线程退出
        primaryStage.setOnCloseRequest(e -> time.stop());

        Scene scene = new Scene(rootNode, _SCENE_WIDTH_, _SCENE_HEIGHT_);
        scene.getStylesheets().add("./fodder/style.css");
        primaryStage.setTitle("图书管理系统");
        primaryStage.setScene(scene);
        primaryStage.getIcons().add(new Image("fodder/查看书籍.png"));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    //管理员可操作
    private static void ShowManageAction() {
        /*按用户等级显示功能按钮
        * 非管理员不可见:
        * 书籍管理 账单管理 账户管理
        * */
        if(!getUser().getGrade().equals("管理员")){
            //数列参数为0开始，choose为1开始 因而不一致
            Function[5].setVisible(false);
            Function[6].setVisible(false);
            Function[7].setVisible(false);
        }
        else{
            Function[5].setVisible(true);
            Function[6].setVisible(true);
            Function[7].setVisible(true);
        }
    }

    static void SelectLayout(MyConnection Myconnection, int number, int choose){
        /* 按choose值进行页面布局设计
        * choose参照值
        * 1：借阅书籍    2：归还书籍      3：购买书籍
        * 4：账号信息    5：历史账单      6：书籍管理
        * 7：账户管理    8：账单管理      9：关于系统
        * 参数:
        * MyConnection:传递数据库连接
        * number:当前功能按钮数目
        * choose:功能按钮布局控制值
        */
        //搜索条
        HBox SearchBox = new HBox();
        HBox SearchBar = new HBox();
        ImageView SearchImage = new ImageView("./fodder/搜索.png");
        SearchImage.setFitHeight(40);
        SearchImage.setPreserveRatio(true);
        TextField SearchText = new TextField();
        SearchText.setPrefSize(300, 40);
        SearchBar.getChildren().addAll(SearchImage, SearchText);
        FunctionBox[number].setPadding(new Insets(10));
        FunctionBox[number].setSpacing(10);

        //搜索按钮
        Button SearchButton = new Button("搜索");
        SearchButton.setPrefSize(_SCENE_WIDTH_/12, _SCENE_HEIGHT_/15);
        HBox SearchAccord = new HBox();
        //搜索方式
        ToggleGroup SearchGroupBook = new ToggleGroup();
        final String[] SelectButton = new String[1];
        ToggleGroup SearchGroupBill = new ToggleGroup();
        ToggleGroup SearchGroupAccount = new ToggleGroup();

        //不同功能布局的搜索方式switch
        // 1：借阅书籍 3：购买书籍 6：书籍管理
        // 书名 作者 ISBN号
        // 2：归还书籍
        // 书名 用户ID
        // 7：账户管理
        // 姓名 账户
        // 8：账单管理
        // 书名 姓名
        switch (choose) {
            //书籍类
            case 1:
            case 3:
            case 6:
                RadioButton SearchAccord_NAME = new RadioButton("书名");
                RadioButton SearchAccord_AUTHOR = new RadioButton("作者");
                RadioButton SearchAccord_ISBN = new RadioButton("ISBN号");
                //设置同一个域
                SearchAccord_NAME.setToggleGroup(SearchGroupBook);
                SearchAccord_AUTHOR.setToggleGroup(SearchGroupBook);
                SearchAccord_ISBN.setToggleGroup(SearchGroupBook);
                SearchAccord_NAME.setSelected(true);//默认书名
                SelectButton[0] = "书名";
                SearchAccord.setSpacing(_SCENE_WIDTH_ / 50);
                SearchAccord.setAlignment(Pos.CENTER);
                SearchAccord.getChildren().addAll(SearchAccord_NAME, SearchAccord_AUTHOR, SearchAccord_ISBN);
                SearchGroupBook.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                            RadioButton r= (RadioButton)newValue;
                            SelectButton[0] = r.getText();
                        });
                break;
            case 2:
                //书籍类
                RadioButton SearchAccord_Name = new RadioButton("书名");
                RadioButton SearchAccord_Id = new RadioButton("ID");
                //设置同一个域
                SearchAccord_Id.setToggleGroup(SearchGroupBill);
                SearchAccord_Name.setToggleGroup(SearchGroupBill);
                SearchAccord_Name.setSelected(true);//默认账户
                SelectButton[0] = "书名";
                SearchAccord.setSpacing(_SCENE_WIDTH_ / 50);
                SearchAccord.setAlignment(Pos.CENTER);
                SearchAccord.getChildren().addAll(SearchAccord_Name, SearchAccord_Id);
                SearchGroupBill.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                            RadioButton r= (RadioButton)newValue;
                            SelectButton[0] = r.getText();
                        });
                break;
            case 7:
                //账户类
                RadioButton SearchAccord_UserName = new RadioButton("姓名");
                RadioButton SearchAccord_UserNumber = new RadioButton("账号");
                //设置同一个域
                SearchAccord_UserName.setToggleGroup(SearchGroupAccount);
                SearchAccord_UserNumber.setToggleGroup(SearchGroupAccount);
                SearchAccord_UserName.setSelected(true);//默认账户
                SelectButton[0] = "姓名";
                SearchAccord.setSpacing(_SCENE_WIDTH_ / 50);
                SearchAccord.setAlignment(Pos.CENTER);
                SearchAccord.getChildren().addAll(SearchAccord_UserName, SearchAccord_UserNumber);
                SearchGroupAccount.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                    RadioButton r= (RadioButton)newValue;
                    SelectButton[0] = r.getText();
                });
                break;
            case 8:
                //账单类
                RadioButton SearchAccord_BookName = new RadioButton("书名");
                RadioButton SearchAccord_AccountName = new RadioButton("姓名");
                //设置同一个域
                SearchAccord_AccountName.setToggleGroup(SearchGroupAccount);
                SearchAccord_BookName.setToggleGroup(SearchGroupAccount);
                SearchAccord_BookName.setSelected(true);//默认账户
                SelectButton[0] = "书名";
                SearchAccord.setSpacing(_SCENE_WIDTH_ / 50);
                SearchAccord.setAlignment(Pos.CENTER);
                SearchAccord.getChildren().addAll(SearchAccord_BookName, SearchAccord_AccountName);
                SearchGroupAccount.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                            RadioButton r= (RadioButton)newValue;
                            SelectButton[0] = r.getText();
                        });
                break;
            default:
        }
        SearchBox.setSpacing(10.0);
        SearchBox.getChildren().addAll(SearchBar, SearchButton, SearchAccord);

        //切换页面按钮和控制台
        HBox PageControl = new HBox();
        /* 切换页面按钮
        * 第一页 上一页 当前页面 / 最大页面 下一页 最后一页
        * 最大页面非翻页时不显示，翻页后加载
        */
        HBox PageChange = new HBox();
        Button FirstPage = new Button("最初一页");
        Button PrePage = new Button("上一页");
        TextField CurPage = new TextField(String.valueOf(_PAGE_BOOK_));
        CurPage.setMaxWidth(40);
        Label Separated = new Label("/");
        Separated.setStyle("-fx-font-size: 24");
        TextField MaxPage = new TextField("");
        MaxPage.setMaxWidth(40);
        MaxPage.setEditable(false);
        Button NextPage = new Button("下一页");
        Button FinalPage = new Button("最后一页");
        PageChange.setSpacing(10);
        PageChange.getChildren().addAll(FirstPage, PrePage, CurPage, Separated, MaxPage, NextPage, FinalPage);
        //结算窗口
        HBox settlement = new HBox();
        Button settlementButton = new Button();
        Button AddByReadTXT = new Button("文件添加");
        PageControl.setSpacing(200);
        PageControl.setAlignment(Pos.CENTER);
        settlementButton.setText("结算");
        if(choose==6) settlement.getChildren().addAll(settlementButton, AddByReadTXT);
        else if(choose==7) {
            AddByReadTXT.setText("添加账户");
            settlement.getChildren().addAll(settlementButton, AddByReadTXT);
        }
        else settlement.getChildren().add(settlementButton);
        settlement.setSpacing(10);
        PageControl.getChildren().addAll(PageChange, settlement);

        //表格展示switch
        //查询数据库数据加载进入列表中，对列表数据进行分割计算最大页码，
        switch (choose){
            case 1:
            case 3:
            case 6:
                //表格显示
                Book[] books = null;
                ArrayList<Book> SelectBooks = new ArrayList<>();
                final ArrayList<Book>[] AllBooks = new ArrayList[1];
                BookDaoImpl bookDaoImpl = new BookDaoImpl(Myconnection);
                AtomicInteger BookMaxLen = new AtomicInteger();
                AtomicInteger BookMaxPage = new AtomicInteger();
                try{
                    AllBooks[0] = bookDaoImpl.getAllBook();
                    getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                    books = AllBooks[0].subList((_PAGE_BOOK_-1)*_MAX_SHOW_COLUMNS_,
                            (_PAGE_BOOK_-1)*_MAX_SHOW_COLUMNS_+ BookMaxLen.get()).toArray(new Book[0]);
                } catch (DaoException e) {
                    e.printStackTrace();
                }

                ObservableList<Book> SelectBook =
                        FXCollections.observableArrayList(books);
                //表格设置
                TableView<Book> BookTableView = new TableView<>();
                BookTableView.setPrefHeight(_TABLEVIEW_HEIGHT_);
                TableColumn BookISBN = new TableColumn("ISBN号");
                TableColumn Name = new TableColumn("书名");
                TableColumn Author = new TableColumn("作者");
                TableColumn Press = new TableColumn("出版社");
                TableColumn Price = new TableColumn("价格");
                TableColumn Stock = new TableColumn("余存量");
                TableColumn Location = new TableColumn("位置");
                TableColumn Label = new TableColumn("类型");
                TableColumn Remark = new TableColumn("备注");
                BookISBN.setCellValueFactory(new PropertyValueFactory<>("ISBN"));
                Name.setCellValueFactory(new PropertyValueFactory<>("Name"));
                Author.setCellValueFactory(new PropertyValueFactory<>("Author"));
                Press.setCellValueFactory(new PropertyValueFactory<>("Press"));
                Price.setCellValueFactory(new PropertyValueFactory<>("Price"));
                Stock.setCellValueFactory(new PropertyValueFactory<>("Stock"));
                Location.setCellValueFactory(new PropertyValueFactory<>("Location"));
                Label.setCellValueFactory(new PropertyValueFactory<>("Label"));
                Remark.setCellValueFactory(new PropertyValueFactory<>("Remark"));
                BookISBN.setPrefWidth(100);Name.setPrefWidth(150);Author.setPrefWidth(100);
                Press.setPrefWidth(200);Price.setPrefWidth(100);
                Stock.setPrefWidth(100);Location.setPrefWidth(200);
                Label.setPrefWidth(200);Remark.setPrefWidth(100);
                BookTableView.setItems(SelectBook);
                BookTableView.getColumns().addAll(BookISBN, Name, Author, Press, Price, Stock, Location, Label, Remark);
                FunctionBox[number].getChildren().addAll(SearchBox, BookTableView, PageControl);
                final SettleUI[] BooksettlementButton = new SettleUI[1];

                //表格双击添加到结算台
                BookTableView.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
                    int time = event.getClickCount();
                    if (time == 2 & (BookTableView.getItems().size() != 0)) {
                        //点击获取当前选择书籍
                        String[] SelectBookStr = BookTableView.getFocusModel().getFocusedItem()
                                .toString().split(",", -1);
                        Book SelectAddBook = new Book(
                                SelectBookStr[0], SelectBookStr[1], SelectBookStr[2], SelectBookStr[3],
                                Integer.parseInt(SelectBookStr[4]), Double.valueOf(SelectBookStr[5]),
                                SelectBookStr[6], SelectBookStr[7], SelectBookStr[8]
                        );
                        SelectAddBook.setNumber(1);Alert alert;int count=0;
                        for (Book selectBook : SelectBooks) {
                            count += selectBook.getNumber();
                        }
                        //循环添加书本（已有数量+1，反之添加）
                        for (int i = 0; i < SelectBooks.size(); i++) {
                            if(Objects.equals(SelectBooks.get(i).getISBN(), SelectAddBook.getISBN())){
                                //大于当前书籍数最大值提示
                                if(SelectBooks.get(i).getNumber() + 1> SelectBooks.get(i).getStock()) {
                                    alert = new Alert(Alert.AlertType.INFORMATION);
                                    alert.setTitle("提示");
                                    alert.setHeaderText("当前数目已经达最大值");
                                    alert.initStyle(StageStyle.UTILITY);//不显示图标
                                    alert.show();
                                }
                                else{
                                    SelectBooks.get(i).setNumber(SelectBooks.get(i).getNumber() + 1);
                                }
                                break;
                            }
                            else if(i==SelectBooks.size()-1){
                                int maxCount = 0;
                                //最大一次借阅数
                                switch (CurUser.getGrade()){
                                    case "学生": maxCount=8;break;
                                    case "老师": maxCount=12;break;
                                    case "管理员": maxCount=20;break;
                                }
                                //大于账户最大一次借阅书籍数的弹窗提示
                                if(count==maxCount){
                                    Alert message = new Alert(Alert.AlertType.INFORMATION);
                                    message.setTitle("提示");
                                    message.setHeaderText("当前已达借阅最大数目");
                                    message.initStyle(StageStyle.UTILITY);//不显示图标
                                    message.show();
                                }
                                else SelectBooks.add(SelectAddBook);
                                break;
                            }
                        }
                        //初始为空，直接添加书籍列表进行初始表格
                        if (SelectBooks.size()==0) SelectBooks.add(SelectAddBook);
                        //分情况判断结算台布局
                        if(choose==1) BooksettlementButton[0] = new SettleUI(SelectBooks, "借阅台", 1);
                        else if(choose==3) BooksettlementButton[0] = new SettleUI(SelectBooks, "结算台", 3);
                        else BooksettlementButton[0] = new SettleUI(SelectBooks, "操作台", 6);
                    }
                });
                //搜索按钮点击搜索
                SearchButton.setOnMouseClicked(e->{
                    Book SearchBook = new Book(
                            "", "", "", "", 0, 0.0, "","",""
                    );
                    String SelectSearchManner = SelectButton[0];
                    switch (SelectSearchManner) {
                        case "书名":
                            SearchBook.setName(SearchText.getText());
                            break;
                        case "作者":
                            SearchBook.setAuthor(SearchText.getText());
                            break;
                        case "ISBN号":
                            SearchBook.setISBN(SearchText.getText());
                            break;
                    }
                    //数据库查询
                    try {
                        AllBooks[0] = bookDaoImpl.getBook(SearchBook, SelectSearchManner);
                        if (AllBooks[0].size()==0){
                            Alert alert = new Alert(Alert.AlertType.ERROR);
                            alert.setTitle("错误");
                            alert.setHeaderText("你所查询的结果为空");
                            alert.initStyle(StageStyle.UTILITY);//不显示图标
                            alert.show();
                        }
                        else {
                            //对当前查询结果进行最大页码计算，并更新表单数据
                            getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                            _PAGE_BOOK_=1;
                            UpdateTableDate(BookTableView, AllBooks[0], BookMaxPage.get(), 1);
                        }
                    } catch (DaoException ex) {
                        ex.printStackTrace();
                    }
                });
                //点击跳转结算窗口
                settlementButton.setOnMouseClicked(e -> {
                    //结算操作界面为空需创建
                    if (BooksettlementButton[0] == null & choose==1)
                        BooksettlementButton[0] = new SettleUI(SelectBooks, "借阅台", 1);
                    else if(BooksettlementButton[0] == null & choose ==3)
                        BooksettlementButton[0] = new SettleUI(SelectBooks, "结算台", 3);
                    else if (BooksettlementButton[0] == null & choose==6)
                        BooksettlementButton[0] = new SettleUI(SelectBooks, "操作台", 6);
                    //加载数据进行展示
                    if(!SettleUI.FirstLoad & choose==1)
                        SettleUI.UpdateTableDate(SettleUI.tableViewBook, SelectBooks, 1);
                    else if(!SettleUI.FirstLoad & choose==3)
                        SettleUI.UpdateTableDate(SettleUI.tableViewBook, SelectBooks, 3);
                    else if (!SettleUI.FirstLoad & choose==6)
                        SettleUI.UpdateTableDate(SettleUI.tableViewBook, SelectBooks, 6);
                    BooksettlementButton[0].display();
                });
                //文件添加书籍按钮
                AddByReadTXT.setOnMouseClicked(e->{
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("文件读取");
                    fileChooser.setInitialDirectory(new File("./fodder")
                    );
                    fileChooser.getExtensionFilters().add(
                            new FileChooser.ExtensionFilter("书本读取", "*.txt")
                    );
                    File file = fileChooser.showOpenDialog(new Stage());
                    BookWriter bw = new BookWriter();
                    try {
                        ArrayList<Book> TxtAddBook = bw.ReadTxt(file);
                        if(TxtAddBook!=null){
                            SettleUI settleUI = new SettleUI(TxtAddBook, "添加书籍", 9);
                            settleUI.display();
                        }
                        else{
                            Alert alert = new Alert(Alert.AlertType.INFORMATION);
                            alert.setTitle("提示");
                            alert.setHeaderText("你本次未选择文件");
                            alert.initStyle(StageStyle.UTILITY);//不显示图标
                            alert.show();
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                });
                //翻页控制
                FirstPage.setOnMouseClicked(e->{
                    _PAGE_BOOK_=1;
                    getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                    MaxPage.setText(String.valueOf(BookMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BOOK_));
                    UpdateTableDate(BookTableView, AllBooks[0], BookMaxPage.get(), 1);
                });
                PrePage.setOnMouseClicked(e->{
                    getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                    _PAGE_BOOK_ = Math.max(_PAGE_BOOK_-1, 1);
                    MaxPage.setText(String.valueOf(BookMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BOOK_));
                    UpdateTableDate(BookTableView, AllBooks[0], BookMaxPage.get(),1);
                });
                NextPage.setOnMouseClicked(e->{
                    getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                    _PAGE_BOOK_ = Math.min(_PAGE_BOOK_+1, BookMaxPage.get());
                    MaxPage.setText(String.valueOf(BookMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BOOK_));
                    UpdateTableDate(BookTableView, AllBooks[0], BookMaxPage.get(),1);
                });
                FinalPage.setOnMouseClicked(e->{
                    getPageLen(AllBooks, BookMaxLen, BookMaxPage, 1);
                    _PAGE_BOOK_= BookMaxPage.get();
                    MaxPage.setText(String.valueOf(BookMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BOOK_));
                    UpdateTableDate(BookTableView, AllBooks[0], BookMaxPage.get(),1);
                });
                break;

            //借阅
            case 2:
                //表格显示
                Record[] records = null;
                final ArrayList<Record>[] AllRecords = new ArrayList[1];
                RecordDaoImpl recordDaoImpl = new RecordDaoImpl(Myconnection);
                BookDaoImpl BookDaoImpl = new BookDaoImpl(Myconnection);
                AtomicInteger RecordMaxLen = new AtomicInteger();
                AtomicInteger RecordMaxPage = new AtomicInteger();
                try{
                    AllRecords[0] = recordDaoImpl.getRecord(CurUser, null,0);
                    getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                    records = AllRecords[0].subList((_PAGE_RECORD_-1)*_MAX_SHOW_COLUMNS_,
                            (_PAGE_RECORD_-1)*_MAX_SHOW_COLUMNS_+ RecordMaxLen.get()).toArray(new Record[0]);
                } catch (DaoException e) {
                    e.printStackTrace();
                }

                ObservableList<Record> SelectRecord =
                        FXCollections.observableArrayList(records);
                //表格设置
                TableView<Record> RecordTableView = new TableView<>();
                RecordTableView.setPrefHeight(_TABLEVIEW_HEIGHT_);
                TableColumn ID = new TableColumn("ID");
                TableColumn AccountNumber = new TableColumn("账号");
                TableColumn AccountName = new TableColumn("名字");
                TableColumn RecordISBN = new TableColumn("ISBN号");
                TableColumn BookName = new TableColumn("书名");
                TableColumn Number = new TableColumn("数量");
                TableColumn LoanTime = new TableColumn("借阅时间");
                TableColumn ReturnTime = new TableColumn("应还时间");
                TableColumn FinishTime = new TableColumn("归还时间");
                TableColumn State = new TableColumn("状态");
                ID.setCellValueFactory(new PropertyValueFactory<>("Id"));
                AccountNumber.setCellValueFactory(new PropertyValueFactory<>("AccountNumber"));
                AccountName.setCellValueFactory(new PropertyValueFactory<>("AccountName"));
                RecordISBN.setCellValueFactory(new PropertyValueFactory<>("ISBN"));
                BookName.setCellValueFactory(new PropertyValueFactory<>("BookName"));
                Number.setCellValueFactory(new PropertyValueFactory<>("Number"));
                LoanTime.setCellValueFactory(new PropertyValueFactory<>("LoanTime"));
                ReturnTime.setCellValueFactory(new PropertyValueFactory<>("ReturnTime"));
                FinishTime.setCellValueFactory(new PropertyValueFactory<>("FinishTime"));
                State.setCellValueFactory(new PropertyValueFactory<>("State"));
                ID.setPrefWidth(100);AccountNumber.setPrefWidth(150);AccountName.setPrefWidth(100);
                RecordISBN.setPrefWidth(150);BookName.setPrefWidth(200);Number.setPrefWidth(100);
                LoanTime.setPrefWidth(120);ReturnTime.setPrefWidth(120);FinishTime.setPrefWidth(120);
                State.setPrefWidth(90);
                RecordTableView.setItems(SelectRecord);
                RecordTableView.getColumns().addAll(ID, AccountNumber, AccountName, RecordISBN, BookName,
                        Number, LoanTime, ReturnTime, FinishTime, State);
                FunctionBox[number].getChildren().addAll(SearchBox, RecordTableView, PageControl);
                settlementButton.setVisible(false);

                //双击还书
                RecordTableView.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
                    int time = event.getClickCount();
                    if (time == 2 & (RecordTableView.getItems().size() != 0)) {
                        String[] SelectRecordStr = RecordTableView.getFocusModel().getFocusedItem()
                                .toString().split(",", -1);
                        Record SelectUpdateRecord = new Record(
                                Integer.parseInt(SelectRecordStr[0]),SelectRecordStr[1],SelectRecordStr[2],
                                SelectRecordStr[3],SelectRecordStr[4],Integer.parseInt(SelectRecordStr[5]),
                                SelectRecordStr[6],SelectRecordStr[7],SelectRecordStr[8],SelectRecordStr[9]
                        );
                        Calendar calendar= Calendar.getInstance();
                        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
                        SelectUpdateRecord.setFinishTime(dateFormat.format(calendar.getTime()));
                        SelectUpdateRecord.setState("已归还");
                        int[] ChangeList = new int[10];
                        ChangeList[8]=ChangeList[9]=1;
                        try {
                            recordDaoImpl.updateRecord(SelectUpdateRecord, ChangeList);
                            Book b = BookDaoImpl.getBookIsbn(SelectUpdateRecord.getISBN());
                            b.setStock(SelectUpdateRecord.getNumber()+b.getStock());
                            BookDaoImpl.updateBook(b);
                            AllRecords[0] = recordDaoImpl.getRecord(CurUser, null,0);
                        } catch (DaoException e) {
                            throw new RuntimeException(e);
                        }
                        //更新数据表单
                        getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                        UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(), 2);
                    }
                });
                //搜索
                SearchButton.setOnMouseClicked(e->{
                    Record SearchRecord = new Record(
                            1,"", "", "", "", 0,
                            "", "","",""
                    );
                    String SelectSearchManner = SelectButton[0];
                    try {
                        switch (SelectSearchManner) {
                            case "书名":
                                SearchRecord.setBookName(SearchText.getText());
                                AllRecords[0] = recordDaoImpl.getRecord(CurUser, SearchRecord,1);
                                break;
                            case "ID":
                                SearchRecord.setId(Integer.parseInt(SearchText.getText()));
                                AllRecords[0] = recordDaoImpl.getRecord(CurUser, SearchRecord,2);
                                break;
                        }
                        if (AllRecords[0].size()==0){
                            Alert alert = new Alert(Alert.AlertType.ERROR);
                            alert.setTitle("错误");
                            alert.setHeaderText("你所查询的结果为空");
                            alert.initStyle(StageStyle.UTILITY);//不显示图标
                            alert.show();
                        }
                        else {
                            getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                            _PAGE_RECORD_=1;
                            UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(), 2);
                        }
                    } catch (DaoException ex) {
                        ex.printStackTrace();
                    }
                });
                //翻页
                FirstPage.setOnMouseClicked(e->{
                    _PAGE_RECORD_=1;
                    getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                    MaxPage.setText(String.valueOf(RecordMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(), 2);
                });
                PrePage.setOnMouseClicked(e->{
                    getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                    _PAGE_RECORD_ = Math.max(_PAGE_RECORD_-1, 1);
                    MaxPage.setText(String.valueOf(RecordMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(),2);
                });
                NextPage.setOnMouseClicked(e->{
                    getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                    _PAGE_RECORD_ = Math.min(_PAGE_RECORD_+1, RecordMaxPage.get());
                    MaxPage.setText(String.valueOf(RecordMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(),2);
                });
                FinalPage.setOnMouseClicked(e->{
                    getPageLen(AllRecords, RecordMaxLen, RecordMaxPage, 2);
                    _PAGE_RECORD_= RecordMaxPage.get();
                    MaxPage.setText(String.valueOf(RecordMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(RecordTableView, AllRecords[0], RecordMaxPage.get(),2);
                });
                break;

            case 5:
            case 8:
                Bill[] bills = null;
                ArrayList<Bill> SelectBills = new ArrayList<>();
                final ArrayList<Bill>[] AllBills = new ArrayList[1];
                BillDaoImpl BillDaoImpl = new BillDaoImpl(Myconnection);
                AtomicInteger BillMaxLen = new AtomicInteger();
                AtomicInteger BillMaxPage = new AtomicInteger();
                try{
                    if(choose==5)AllBills[0] = BillDaoImpl.getBill(null, CurUser, "账号", 5);
                    else AllBills[0] = BillDaoImpl.getAllBill();
                    getPageLen(AllBills, BillMaxLen, BillMaxPage, 3);
                    bills = AllBills[0].subList((_PAGE_BILL_-1)*_MAX_SHOW_COLUMNS_,
                            (_PAGE_BILL_-1)*_MAX_SHOW_COLUMNS_+BillMaxLen.get()).toArray(new Bill[0]);
                } catch (DaoException e) {
                    e.printStackTrace();
                }

                ObservableList<Bill> SelectBill =
                        FXCollections.observableArrayList(bills);
                TableView<Bill> BillTableView = new TableView<>();
                BillTableView.setPrefHeight(_TABLEVIEW_HEIGHT_);
                TableColumn Id = new TableColumn("ID");
                TableColumn BillBookName = new TableColumn("书名");
                TableColumn BillAuthor = new TableColumn("作者");
                TableColumn Account = new TableColumn("账号");
                TableColumn BillAccountName = new TableColumn("姓名");
                TableColumn BillNumber = new TableColumn("数量");
                TableColumn BillPrice = new TableColumn("价格");
                TableColumn Date = new TableColumn("时间");
                Id.setCellValueFactory(new PropertyValueFactory<>("Id"));
                BillBookName.setCellValueFactory(new PropertyValueFactory<>("BookName"));
                BillAuthor.setCellValueFactory(new PropertyValueFactory<>("Author"));
                Account.setCellValueFactory(new PropertyValueFactory<>("Account"));
                BillAccountName.setCellValueFactory(new PropertyValueFactory<>("AccountName"));
                BillNumber.setCellValueFactory(new PropertyValueFactory<>("Number"));
                BillPrice.setCellValueFactory(new PropertyValueFactory<>("Price"));
                Date.setCellValueFactory(new PropertyValueFactory<>("Date"));
                Id.setPrefWidth(100);BillBookName.setPrefWidth(200);
                BillAuthor.setPrefWidth(200);Account.setPrefWidth(150);
                BillAccountName.setPrefWidth(150);BillNumber.setPrefWidth(150);BillPrice.setPrefWidth(150);
                Date.setPrefWidth(150);BillTableView.setItems(SelectBill);

                BillTableView.getColumns().addAll(Id, BillBookName, BillAuthor, Account, BillAccountName,
                        BillNumber, BillPrice, Date);
                FunctionBox[number].getChildren().addAll(SearchBox, BillTableView, PageControl);
                final SettleUI[] BillSettlementButtonWin = new SettleUI[1];

                if (choose == 8) {
                    //表格点击添加
                    BillTableView.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
                        int time = event.getClickCount();
                        if (time == 2 & (BillTableView.getItems().size() != 0)) {
                            String[] SelectBillStr = BillTableView.getFocusModel().getFocusedItem()
                                    .toString().split(",", -1);
                            Bill SelectAddBill = new Bill(
                                    Integer.parseInt(SelectBillStr[0]), SelectBillStr[1], SelectBillStr[2],
                                    SelectBillStr[3], SelectBillStr[4], Integer.parseInt(SelectBillStr[5]),
                                    Double.valueOf(SelectBillStr[6]), SelectBillStr[7]
                            );
                            if (SelectBills.size() == 0) SelectBills.add(SelectAddBill);
                        }
                    });
                    //搜索点击
                    SearchButton.setOnMouseClicked(e -> {
                        Bill SearchBill = new Bill(
                                1, "", "", "", "", 0, 0.0, ""
                        );
                        String SelectSearchManner = SelectButton[0];
                        switch (SelectSearchManner) {
                            case "书名":
                                SearchBill.setBookName(SearchText.getText());
                                break;
                            case "姓名":
                                SearchBill.setAccountName(SearchText.getText());
                                break;
                        }
                        try {
                            AllBills[0] = BillDaoImpl.getBill(SearchBill, CurUser, SelectSearchManner, 8);
                            InformationAlert(AllBills, BillMaxLen, BillMaxPage, BillTableView);
                        } catch (DaoException ex) {
                            ex.printStackTrace();
                        }
                    });
                    //操作台
                    settlementButton.setOnMouseClicked(e->{
                        if(BillSettlementButtonWin[0]==null)
                            BillSettlementButtonWin[0]=new SettleUI(SelectBills, "操作台", 8);
                        if(!SettleUI.FirstLoad)
                            SettleUI.UpdateTableDate(SettleUI.tableViewBill, SelectBills, 8);
                        BillSettlementButtonWin[0].display();
                    });
                }
                else {
                    settlementButton.setVisible(false);
                    //搜索点击
                    SearchButton.setOnMouseClicked(e -> {
                        Bill SearchBill = new Bill(
                                1, "", "", "", "", 0, 0.0, ""
                        );
                        String SelectSearchManner = SelectButton[0];
                        switch (SelectSearchManner) {
                            case "书名":
                                SearchBill.setBookName(SearchText.getText());
                                break;
                            case "姓名":
                                SearchBill.setAccountName(SearchText.getText());
                                break;
                        }
                        try {
                            AllBills[0] = BillDaoImpl.getBill(SearchBill, CurUser, SelectSearchManner, 6);
                            InformationAlert(AllBills, BillMaxLen, BillMaxPage, BillTableView);
                        } catch (DaoException ex) {
                            ex.printStackTrace();
                        }
                    });
                }
                //翻页
                FirstPage.setOnMouseClicked(e -> {
                    _PAGE_BILL_ = 1;
                    getPageLen(AllBills, BillMaxLen, BillMaxPage, 3);
                    MaxPage.setText(String.valueOf(BillMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(BillTableView, AllBills[0], BillMaxPage.get(), 3);
                });
                PrePage.setOnMouseClicked(e -> {
                    getPageLen(AllBills, BillMaxLen, BillMaxPage, 2);
                    _PAGE_BILL_ = Math.max(_PAGE_BILL_ - 1, 1);
                    MaxPage.setText(String.valueOf(BillMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BILL_));
                    UpdateTableDate(BillTableView, AllBills[0], BillMaxPage.get(), 3);
                });
                NextPage.setOnMouseClicked(e -> {
                    getPageLen(AllBills, BillMaxLen, BillMaxPage, 3);
                    _PAGE_BILL_ = Math.min(_PAGE_BILL_ + 1, BillMaxPage.get());
                    MaxPage.setText(String.valueOf(BillMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BILL_));
                    UpdateTableDate(BillTableView, AllBills[0], BillMaxPage.get(), 3);
                });
                FinalPage.setOnMouseClicked(e -> {
                    getPageLen(AllBills, BillMaxLen, BillMaxPage, 3);
                    _PAGE_RECORD_ = BillMaxPage.get();
                    MaxPage.setText(String.valueOf(BillMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_BILL_));
                    UpdateTableDate(BillTableView, AllBills[0], BillMaxPage.get(), 3);
                });
                break;

            case 7:
                User[] users = null;
                ArrayList<User> SelectUsers = new ArrayList<>();
                final ArrayList<User>[] AllUsers = new ArrayList[1];
                AccountDaoImpl AccountDaoImpl = new AccountDaoImpl(Myconnection);
                AtomicInteger UserMaxLen = new AtomicInteger();
                AtomicInteger UserMaxPage = new AtomicInteger();
                try{
                    AllUsers[0] = AccountDaoImpl.getAllAccount();
                    getPageLen(AllUsers, UserMaxLen, UserMaxPage, 4);
                    users = AllUsers[0].subList((_PAGE_ACCOUNT_-1)*_MAX_SHOW_COLUMNS_,
                            (_PAGE_ACCOUNT_-1)*_MAX_SHOW_COLUMNS_+UserMaxLen.get()).toArray(new User[0]);
                } catch (DaoException e) {
                    e.printStackTrace();
                }

                ObservableList<User> SelectUser =
                        FXCollections.observableArrayList(users);
                TableView<User> AccountTableView = new TableView<>();
                AccountTableView.setPrefHeight(_TABLEVIEW_HEIGHT_);
                TableColumn UserNumber = new TableColumn("账号");
                TableColumn UserName = new TableColumn("姓名");
                TableColumn UserSexy = new TableColumn("性别");
                TableColumn UserClass = new TableColumn("班级");
                TableColumn UserTitle = new TableColumn("职称");
                TableColumn UserFaction = new TableColumn("系别");
                TableColumn UserGrade = new TableColumn("权限");
                UserNumber.setCellValueFactory(new PropertyValueFactory<>("Number"));
                UserName.setCellValueFactory(new PropertyValueFactory<>("Name"));
                UserSexy.setCellValueFactory(new PropertyValueFactory<>("Sexy"));
                UserClass.setCellValueFactory(new PropertyValueFactory<>("discipline"));
                UserTitle.setCellValueFactory(new PropertyValueFactory<>("Title"));
                UserFaction.setCellValueFactory(new PropertyValueFactory<>("Faction"));
                UserGrade.setCellValueFactory(new PropertyValueFactory<>("Grade"));
                UserNumber.setPrefWidth(200);UserName.setPrefWidth(200);
                UserSexy.setPrefWidth(150);UserClass.setPrefWidth(150);
                UserTitle.setPrefWidth(100);UserFaction.setPrefWidth(200);
                UserGrade.setPrefWidth(150);
                AccountTableView.getColumns().addAll(UserNumber, UserName, UserSexy,
                        UserClass, UserTitle, UserFaction, UserGrade);
                AccountTableView.setItems(SelectUser);
                FunctionBox[number].getChildren().addAll(SearchBox, AccountTableView, PageControl);

                final SettleUI[] UserSettleWin = new SettleUI[1];

                //表格点击事件
                AccountTableView.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
                    int time = event.getClickCount();
                    if (time == 2 & (AccountTableView.getItems().size() != 0)) {
                        String[] SelectRecordStr = AccountTableView.getFocusModel().getFocusedItem()
                                .toString().split(",", -1);
                        User SelectAddUser = new User(
                                SelectRecordStr[0],SelectRecordStr[1],SelectRecordStr[2],
                                SelectRecordStr[3],SelectRecordStr[4],SelectRecordStr[5],
                                SelectRecordStr[6],SelectRecordStr[7]
                        );
                        if(SelectUsers.size()==0) SelectUsers.add(SelectAddUser);
                        for (int i=0;i<SelectUsers.size();i++) {
                            User u = SelectUsers.get(i);
                            if(i==SelectUsers.size()-1&!Objects.equals(u.getNumber(), SelectAddUser.getNumber()))
                                SelectUsers.add(SelectAddUser);
                        }
                        UserSettleWin[0] = new SettleUI(SelectUsers, "操作台", 7);
                    }
                });
                //操作台
                settlementButton.setOnMouseClicked(e -> {
                    if(UserSettleWin[0]==null)
                        UserSettleWin[0]  =new SettleUI(SelectUsers, "操作台", 7);
                    if(!SettleUI.FirstLoad)
                        SettleUI.UpdateTableDate(SettleUI.tableViewAccount, SelectUsers, 2);
                    UserSettleWin[0].display();
                });
                //读取文件进入数据库
                AddByReadTXT.setOnMouseClicked(e->{
                    ChangeUI changeUI = new ChangeUI();
                    changeUI.display(new Stage(), 3,
                            new Book("", "","", "", 0, 0.0, "", "", "")
                    );
                });
                //翻页
                FirstPage.setOnMouseClicked(e -> {
                    _PAGE_ACCOUNT_ = 1;
                    getPageLen(AllUsers, UserMaxLen, UserMaxPage, 4);
                    MaxPage.setText(String.valueOf(UserMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_RECORD_));
                    UpdateTableDate(AccountTableView, AllUsers[0], UserMaxPage.get(), 4);
                });
                PrePage.setOnMouseClicked(e -> {
                    getPageLen(AllUsers, UserMaxLen, UserMaxPage, 4);
                    _PAGE_ACCOUNT_ = Math.max(_PAGE_ACCOUNT_ - 1, 1);
                    MaxPage.setText(String.valueOf(UserMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_ACCOUNT_));
                    UpdateTableDate(AccountTableView, AllUsers[0], UserMaxPage.get(), 4);
                });
                NextPage.setOnMouseClicked(e -> {
                    getPageLen(AllUsers, UserMaxLen, UserMaxPage, 4);
                    _PAGE_ACCOUNT_ = Math.min(_PAGE_ACCOUNT_ + 1, UserMaxPage.get());
                    MaxPage.setText(String.valueOf(UserMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_ACCOUNT_));
                    UpdateTableDate(AccountTableView, AllUsers[0], UserMaxPage.get(), 4);
                });
                FinalPage.setOnMouseClicked(e -> {
                    getPageLen(AllUsers, UserMaxLen, UserMaxPage, 4);
                    _PAGE_RECORD_ = UserMaxPage.get();
                    MaxPage.setText(String.valueOf(UserMaxPage));
                    CurPage.setText(String.valueOf(_PAGE_ACCOUNT_));
                    UpdateTableDate(AccountTableView, AllUsers[0], UserMaxPage.get(), 4);
                });
                break;
            default:
        }
    }

    private static void InformationAlert(ArrayList<Bill>[] allBills, AtomicInteger billMaxLen,
                                         AtomicInteger billMaxPage, TableView<Bill> billTableView) {
        //对于查询结果进行提示和表单刷新
        if (allBills[0].size() == 0) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText("你所查询的结果为空");
            alert.initStyle(StageStyle.UTILITY);//不显示图标
            alert.show();
        } else {
            getPageLen(allBills, billMaxLen, billMaxPage, 3);
            _PAGE_BILL_ = 1;
            UpdateTableDate(billTableView, allBills[0], billMaxPage.get(), 3);
        }
    }

    static void AccountLayout(MyConnection Myconnection, int number){
        /*账户信息布局
        * 上端图片
        * 用户类别 用户姓名 用户密码 用户账号
        * 点击刷新 可于密码框进行密码修改
        * 点击确定 上传密码框当前信息
        * 参数:
        * MyConnection:数据库连接
        * number:当前功能按钮数目
        */
        Random random = new Random();
        String BackIMG = "./fodder/"+ random.nextInt(_Image_MAX_NUMBER_) +".jpg";
        //上端图片
        Image Blue_bg = new Image(BackIMG);
        ImageView imageview = new ImageView(Blue_bg);
        imageview.setFitHeight(430);imageview.setFitWidth(900);

        HBox Line1 = new HBox();
        HBox IDBox = new HBox();
        Label ID = new Label("账户:");
        TextField IDText = new TextField(getUser().getNumber());
        IDText.setEditable(false);
        IDBox.getChildren().addAll(ID, IDText);
        HBox NameBox = new HBox();
        Label Name = new Label("姓名:");
        TextField NameText = new TextField(getUser().getName());
        NameBox.getChildren().addAll(Name, NameText);
        NameText.setEditable(false);
        Line1.setSpacing(30);Line1.setPadding(new Insets(10));
        Line1.getChildren().addAll(IDBox, NameBox);

        HBox Line2 = new HBox();
        HBox PasswordBox = new HBox();
        Label Password = new Label("密码:");
        TextField PasswordText = new TextField("**********");
        PasswordText.setEditable(false);
        PasswordBox.getChildren().addAll(Password, PasswordText);
        HBox SexBox = new HBox();
        Label Sex = new Label("性别:");
        TextField SexText = new TextField(getUser().getSexy());
        SexText.setEditable(false);
        SexBox.getChildren().addAll(Sex, SexText);
        Line2.setSpacing(30);Line2.setPadding(new Insets(10));
        Line2.getChildren().addAll(PasswordBox, SexBox);

        //当前用户等级
        HBox UserBox = new HBox();
        ToggleGroup UserGroup = new ToggleGroup();
        RadioButton User_Student = new RadioButton("学生");
        RadioButton User_Teacher = new RadioButton("老师");
        RadioButton User_Admin = new RadioButton("admin");
        //设置同一个域
        User_Student.setToggleGroup(UserGroup);
        User_Teacher.setToggleGroup(UserGroup);
        User_Admin.setToggleGroup(UserGroup);
        if(getUser().getGrade().equals("学生"))
            UserGroup.selectToggle(User_Student);
        else if (getUser().getGrade().equals("老师"))
            UserGroup.selectToggle(User_Teacher);
        else UserGroup.selectToggle(User_Admin);

        //按钮设置
        HBox Line3 = new HBox();
        Button ChangeAccount = new Button("修改");
        Button ChangeConfirm = new Button("确认");
        Line3.setSpacing(30);
        Line3.getChildren().addAll(ChangeAccount, ChangeConfirm);
        Line3.getStylesheets().add("./fodder/style.css");

        Label MaxLoan = new Label();
        if(getUser().getGrade().equals("学生"))
            MaxLoan.setText("最大借阅数为:8");
        else if (getUser().getGrade().equals("老师"))
            MaxLoan.setText("最大借阅数为:12");
        else MaxLoan.setText("最大借阅数为: 20");
        UserBox.setSpacing(30);UserBox.setPadding(new Insets(10));
        UserBox.getChildren().addAll(User_Student, User_Teacher, User_Admin, MaxLoan);

        //侧方功能按钮样式设置
        FunctionBox[number].setSpacing(20);
        FunctionBox[number].setPadding(new Insets(10));
        FunctionBox[number].getStylesheets().add("./fodder/style2.css");
        FunctionBox[number].getChildren().addAll(imageview, Line1, Line2, UserBox, Line3);

        //修改密码
        ChangeAccount.setOnMouseClicked(e-> PasswordText.setEditable(true));
        //密码上传
        ChangeConfirm.setOnMouseClicked(e->{
            if(!PasswordText.getText().equals("**********")){//默认数据排除
                AccountDaoImpl accountDao = new AccountDaoImpl(Myconnection);
                try {
                    getUser().setPassword(PasswordText.getText());
                    accountDao.updateAccount(getUser(), 1);
                    PasswordText.setText("账户已修改密码");
                } catch (DaoException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    static void SystemInf(int number){
        //系统信息提醒
        VBox rootNode = new VBox();
        Font font = new Font("微软雅黑", 28);
        Font font_smi = new Font("微软雅黑", 22);
        Label Version = new Label("当前系统版本为V1.0.0\n");
        Version.setFont(font);
        Label Message = new Label("温馨提示:");
        Message.setFont(font);
        Label Instructions = new Label("1.部分功能操作完后并不会立即对当前页面的数据进行更新,可点击返回或刷新进行数据刷新\n" +
                "2.本系统只支持该校已有学号/工号的学生/教职工进行注册登录,其余人员需先向校方申请学号/工号\n" +
                "3.结算平台点击下方按钮方可切换当前操作模式,初始为取消当前选择,切换后双击即可操作\n" +
                "\n感谢你使用本系统\n后序系统将逐步优化完善功能\n");
        Image image = new Image("./fodder/感谢.jfif");
        ImageView imageView = new ImageView(image);

        Instructions.setFont(font_smi);
        rootNode.getChildren().addAll(Version, Message, Instructions, imageView);
        rootNode.setPadding(new Insets(10));
        rootNode.setSpacing(10);
        FunctionBox[number] = rootNode;
    }

    private static void getPageLen(ArrayList[] all, AtomicInteger MaxLen, AtomicInteger MaxPage, int choose) {
        /*获取当前界面的最大长度
        * 传递参数
        * all:当前查询选择所有目标元素的数列
        * MaxLen:传递当前选择的最大长度
        * MaxPage:传递当前选择的最大页码
        * choose:选择查询类型
        * 1:书籍 2:借阅记录 3:账单 4:账号
        */
        int page = 0;
        switch (choose){
            case 1:
                page = _PAGE_BOOK_;
                break;
            case 2:
                page = _PAGE_RECORD_;
                break;
            case 3:
                page = _PAGE_BILL_;
                break;
            case 4:
                page = _PAGE_ACCOUNT_;
                break;
            default:
                System.out.println("最大长度获取传递参数错误");
        }
        if(all[0].size()%_MAX_SHOW_COLUMNS_==0) MaxPage.set(all[0].size() / _MAX_SHOW_COLUMNS_);
        else MaxPage.set(all[0].size() / _MAX_SHOW_COLUMNS_ + 1);
        if((page != MaxPage.get())&MaxPage.get()!=0) MaxLen.set(_MAX_SHOW_COLUMNS_);
        else MaxLen.set(all[0].size() % (_MAX_SHOW_COLUMNS_+1));
    }
    public static void UpdateTableDate(TableView tableView, ArrayList arrayList, int maxPage, int choose){
        //按照choose值更新表单数据
        //choose参照值:
        //1.书籍 2.借阅记录 3.账单记录 4.账户记录
        //参数
        // tableView:需要更新数据的表单控件
        // arrayList:需要进行的最新表单数据
        // maxPage:当前类别的最大页码
        // choose:选择类别值
        int MaxLen;
        switch (choose){
            case 1:
                if(_PAGE_BOOK_ == maxPage | _PAGE_BOOK_>maxPage)//当前为最大页码进行展示最大计算
                    MaxLen = arrayList.size()-(_PAGE_BOOK_-1)*_MAX_SHOW_COLUMNS_;
                else MaxLen=_MAX_SHOW_COLUMNS_;
                //创建新的书籍页码列表
                Book[] books = (Book[]) arrayList.subList((_PAGE_BOOK_-1)*_MAX_SHOW_COLUMNS_,
                        (_PAGE_BOOK_-1)*_MAX_SHOW_COLUMNS_+MaxLen).toArray(new Book[0]);
                ObservableList<Book> SelectBook = FXCollections.observableArrayList(books);
                //加载新的数据
                ((TableView<Book>) tableView).setItems(SelectBook);
                break;
            case 2:
                if(_PAGE_RECORD_ == maxPage | _PAGE_RECORD_>maxPage)
                    MaxLen = arrayList.size()-(_PAGE_RECORD_-1)*_MAX_SHOW_COLUMNS_;
                else MaxLen=_MAX_SHOW_COLUMNS_;
                Record[] records = (Record[]) arrayList.subList((_PAGE_RECORD_-1)*_MAX_SHOW_COLUMNS_,
                        (_PAGE_RECORD_-1)*_MAX_SHOW_COLUMNS_+MaxLen).toArray(new Record[0]);
                ObservableList<Record> SelectRecord = FXCollections.observableArrayList(records);
                ((TableView<Record>) tableView).setItems(SelectRecord);
                break;
            case 3:
                if(_PAGE_BILL_==maxPage | _PAGE_BILL_>maxPage)
                    MaxLen = arrayList.size()-(_PAGE_BILL_-1)*_MAX_SHOW_COLUMNS_;
                else MaxLen=_MAX_SHOW_COLUMNS_;
                Bill[] bills = (Bill[]) arrayList.subList((_PAGE_BILL_-1)*_MAX_SHOW_COLUMNS_,
                        (_PAGE_BILL_-1)*_MAX_SHOW_COLUMNS_+MaxLen).toArray(new Bill[0]);
                ObservableList<Bill> SelectBill = FXCollections.observableArrayList(bills);
                ((TableView<Bill>) tableView).setItems(SelectBill);
                break;
            case 4:
                if(_PAGE_ACCOUNT_==maxPage | _PAGE_ACCOUNT_>maxPage)
                    MaxLen = arrayList.size()-(_PAGE_ACCOUNT_-1)*_MAX_SHOW_COLUMNS_;
                else MaxLen=_MAX_SHOW_COLUMNS_;
                User[] users = (User[]) arrayList.subList((_PAGE_ACCOUNT_-1)*_MAX_SHOW_COLUMNS_,
                        (_PAGE_ACCOUNT_-1)*_MAX_SHOW_COLUMNS_+MaxLen).toArray(new User[0]);
                ObservableList<User> SelectUser = FXCollections.observableArrayList(users);
                ((TableView<User>) tableView).setItems(SelectUser);
                break;
        }
    }

    static void UpdateWindows(BorderPane rootNode){
        //刷新当前所有的子界面元素
        //对数据元素进行重新生成（可优化）
        FunctionBox = new VBox[_MAX_FACTION_];
        for (int i = 0;i<FunctionBox.length;i++) {
            FunctionBox[i] = new VBox();
            Function[i].setStyle(_BackgroundColor_);
        }
        ShowManageAction();//设置不可见按钮
        _CUR_BOX_FACTION = 0;//初始化功能按钮个数
        SelectLayout(Myconnection, _CUR_BOX_FACTION++,1);
        rootNode.setCenter(FunctionBox[0]);//默认借阅数据为中心加载数据
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 2);
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 3);
        AccountLayout(Myconnection, _CUR_BOX_FACTION++);
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 5);
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 6);
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 7);
        SelectLayout(Myconnection, _CUR_BOX_FACTION++, 8);
        SystemInf(_CUR_BOX_FACTION++);
    }
    static User getUser() {return CurUser;}
    static void setUser(User curUser) {CurUser = curUser;}
    public static MyConnection getMyConnection() {return Myconnection;}
    public static void setMyConnection(MyConnection myconnection) {Myconnection = myconnection;}
    public static Stage getLogin() {return login;}
    public static void setLogin(Stage login) {ManagerUI.login = login;}
    public static Stage getManagerStage() {return ManagerStage;}
    public static void setManagerStage(Stage managerStage) {ManagerStage = managerStage;}
    public static void updateUser(User user){
        setUser(user);
        AccountName.setText(String.format("当前用户: %s", getUser().getName()));
    }
}
